// +build ignore

package main

import (
	"fmt"
	"log"
	"os"
	"sync"
	"time"

	"gopl/goroutine/thumbnail"
)

func makeThumbnails(filenames <-chan string) int64 {
	var wg sync.WaitGroup
	sizes := make(chan int64)
	for f := range filenames {
		wg.Add(1)
		go func(f string) {
			defer wg.Done()
			thumb, err := thumbnail.ImageFile(f)
			if err != nil {
				log.Println(err)
				return
			}
			info, _ := os.Stat(thumb)
			sizes <- info.Size()
		}(f)
	}

	go func() {
		wg.Wait()
		close(sizes)
	}()

	var total int64
	for size := range sizes {
		total += size
	}
	return total
}

func main() {
	now := time.Now()
	files := make(chan string)

	go func() {
		for _, file := range os.Args[1:] {
			files <- file
		}
		close(files)
	}()
	total := makeThumbnails(files)
	fmt.Printf("total size:%d\n", total)
	fmt.Printf("elapse:%.3f ms\n", 1000*time.Since(now).Seconds())
}
